﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model.Database;
using SearchModel;

namespace Bll
{
    partial class TreeDataHeadBll
    {
        /// <summary>
        /// 条件分页查询
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="total"></param>
        /// <param name="smTreeDataHead"></param>
        /// <returns></returns>
        public List<TreeDataHead> GetListBySearchPage(int pageIndex, int pageSize, out int total, SMTreeDataHead smTreeDataHead)
        {
            IQueryable<TreeDataHead> treeDataHeadIq = CurrentDal.GetIQueryable();

            treeDataHeadIq = BaseSearch(treeDataHeadIq, smTreeDataHead);
            total = treeDataHeadIq.Count();
            treeDataHeadIq = treeDataHeadIq.OrderBy(a => a.Sort).ThenByDescending(a => a.AddTime);
            treeDataHeadIq = treeDataHeadIq.Skip((pageIndex - 1) * pageSize).Take(pageSize);//分页前必须排序，不然EF报错

            return treeDataHeadIq.ToList();
        }
        /// <summary>
        /// 重置树状数据（重置缓存）
        /// </summary>
        public void ResetTreeData()
        {
            
        }








        #region 私有方法
        /// <summary>
        /// 条件查询
        /// </summary>
        /// <param name="treeDataHeadIq"></param>
        /// <param name="smTreeDataHead"></param>
        /// <returns></returns>
        private IQueryable<TreeDataHead> BaseSearch(IQueryable<TreeDataHead> treeDataHeadIq, SMTreeDataHead smTreeDataHead)
        {
            if (!string.IsNullOrEmpty(smTreeDataHead.Name))
            {
                treeDataHeadIq=treeDataHeadIq.Where(a => a.Name == smTreeDataHead.Name);
            }

            return treeDataHeadIq;
        }
        #endregion

    }
}
